package com.habdqn.mybatis.demo.dao;

import com.alibaba.fastjson.JSON;
import com.habdqn.mybatis.demo.entity.Department;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author Xing Jiejian 2019-10-28
 */
public class DepartmentDaoTest2 {
    private SqlSession sqlSession;
    private DepartmentDao dao;


    @Before
    public void setUp() throws Exception {
        sqlSession = MybatisUti.createSqlSession();
        dao = sqlSession.getMapper(DepartmentDao.class);
    }

    @After
    public void tearDown() throws Exception {
        sqlSession.close();
    }

    @Test
    public void save() {
        Department department = new Department("财务部");
        int id = dao.save(department);
        System.out.println(id);
        sqlSession.commit();
    }

    @Test
    public void queryById() {
        Department department = dao.queryById(1);
        System.out.println(department);
    }

    @Test
    public void queryByLimit() {
        List<Department> depts = dao.queryByLimit(1, 2);
        System.out.println(JSON.toJSONString(depts));
    }

    @Test
    public void saveDepts() {
        List<Department> depts = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            depts.add(new Department("测试部门" + i));
        }
        dao.saveDepts(depts);
        sqlSession.commit();
    }
}